home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / xgakit.exe / XGADEMO.C < prev    next >
C/C++ Source or Header  |  1991-05-06  |  6KB  |  230 lines

  1. /*
  2.     XGAKIT package demo program
  3.     This program just demonstrates the use of the XGAKIT routines
  4.  
  5.     by Bert Tyler of Tyler Software
  6.     CIS ID:  73477,433
  7.     (from Internet/BITNET: 73477.433@compuserve.com)
  8.     
  9. */
  10.  
  11. unsigned int intpixels[1024];    /* pixel arrays (can hold a scan line) */
  12. unsigned char pixels[1024];
  13.  
  14. unsigned char palette[512][3];    /* palette values */
  15.  
  16. int debug = 0;            /* for my own testing purposes */
  17.  
  18. main() {
  19. int capabilities, i, j;
  20.  
  21. /*  so, does this guy really have an XGA adapter? */
  22. if (! (capabilities = xga_detect())) {
  23.     puts("...but you have no XGA adapter\n");
  24.     exit(1);
  25.     }
  26.  
  27. /* make a fancy palette with 64 shades of red, green, blue, then grey */
  28. /* then double it up to make color-cycling easier */
  29. for (i = 0; i < 256; i++)
  30.     for (j = 0; j < 3; j++)
  31.         palette[i][j] = 0;
  32. for (i = 0; i < 64; i++) {
  33.     j = i << 2;
  34.     palette[i    ][0] = palette[i+64 ][1] = palette[i+128][2] = j; 
  35.     palette[i+192][0] = palette[i+192][1] = palette[i+192][2] = j; 
  36.     }
  37. for (i = 0; i < 256; i++)
  38.     for (j = 0; j < 3; j++)
  39.         palette[i+256][j] = palette[i][j];
  40.  
  41. /* loop until he chooses option 0 (or presses the ESC key) */
  42. for (;;) {
  43.  
  44.     system("cls");
  45.  
  46.     puts("XGAKIT Demo program");
  47.     puts("by Bert Tyler  (CIS ID: 73477,433 - 73477.433@compuserve.com)");
  48.     puts("");
  49.  
  50.     /* tell the user what his hardware looks like */
  51.     if ((capabilities & 8) == 0)
  52.         puts("\nYou have an XGA adapter with 512K of memory");
  53.     else
  54.         puts("\nYou have an XGA adapter with 1MB of memory");
  55.     if ((capabilities & 4) == 0)
  56.         puts("     It is attached to a low-rez monitor");
  57.     else
  58.         puts("     It is attached to a high-rez monitor");
  59.     if ((capabilities & 2) == 0)
  60.         puts("     The monitor is a monochrome monitor");
  61.     else
  62.         puts("     The monitor is a color monitor");
  63.     if ((capabilities & 16) == 0)
  64.         puts("     The XGA is currently also your VGA adapter");
  65.     else
  66.         puts("     The XGA is part of a dual-monitor setup");
  67.  
  68.     puts("\n\n What do you want to do?");
  69.     puts("   0 = end this demo");
  70.     puts("   1 = demo 132-column    text     mode");
  71.     puts("   2 = demo 1024x768x256  graphics mode  (a,b)");
  72.     puts("   3 = demo 1024x768x16   graphics mode  (b)");
  73.     puts("   4 = demo 640x480x256   graphics mode");
  74.     puts("   5 = demo 640x480x65536 graphics mode  (a)");
  75.     puts("   6 = demo 800x600x16    graphics mode  (c)");
  76.     puts("   7 = demo 800x600x256   graphics mode  (c)");
  77. /*
  78.         (when I get this one working, I'll get rid of this comment)
  79.     puts("   8 = demo 800x600x65536 graphics mode  (c)");
  80. */
  81.     puts(" ");
  82.     puts("(a) - requires 1MB of video memory");
  83.     puts("(b) - requires a 1024x768-capable monitor");
  84.     puts("(c) - requires an 800x600-capable monitor");
  85.     puts(" ");
  86.  
  87.     i = getch();
  88.     j = i - '0';
  89.     switch(i) {
  90.         case '0':
  91.         case 27 :
  92.             exit(0);        /* end-of-program */
  93.             break;
  94.         case '1':
  95.             if ((capabilities & 16) == 0) {
  96.                 xga_mode(1);    /* 132-char text mode */
  97.                 puts("\n\nThis is the XGA's DOS-compatible 132-column text mode");
  98.                 puts("C programs can write to the screen with dull-normal 'printf()' and 'puts()' statements");
  99.                 }
  100.             else
  101.                 puts("That can only be done on a single-monitor setup\n");
  102.             puts("\n\n Press any key to continue...");
  103.             break;
  104.         case '2':
  105.         case '3':
  106.         case '4':
  107.         case '5':
  108.         case '6':
  109.         case '7':
  110.         case '8':
  111.             if (!xga_mode(j)) {    /* graphics modes */
  112.                 puts("\n\n Your hardware can't run that mode");
  113.                 puts("\n\n Press any key to continue...");
  114.                 }
  115.             else    {
  116.                 if (j == 2) demoscreen(1024, 768, 256);
  117.                 if (j == 3) demoscreen(1024, 768, 16);
  118.                 if (j == 4) demoscreen(640,  480, 256);
  119.                 if (j == 5) demoscreen(640,  480, -1);
  120.                 if (j == 6) demoscreen(800,  600, 16);
  121.                 if (j == 7) demoscreen(800,  600, 256);
  122.                 if (j == 8) demoscreen(800,  600, -1);
  123.                 }
  124.             break;
  125.         /* debugging code leftover from testing this stuff */
  126.         case 'd':
  127.         case 'D':
  128.             debug = 1 - debug;    /* flip-flop debug flag */
  129.             break;
  130.         default :
  131.             break;
  132.         }
  133. /*
  134.     puts("\007");
  135. */
  136.     getch();
  137.     xga_mode(0);    /* switch back to VGA-compatible text mode */
  138.  
  139.     /* debugging code leftover from testing this stuff */
  140.     if (debug && j >= 2 && j <= 5) {    /* prove we can read */
  141.         for (i = 0; i < 100; i+= 10)    /*    pixels, too */
  142.         printf(" %d %d %d %d %d %d %d %d %d %d\n",
  143.             intpixels[i+0],intpixels[i+1],intpixels[i+2],
  144.             intpixels[i+3],intpixels[i+4],intpixels[i+5],
  145.             intpixels[i+6],intpixels[i+7],intpixels[i+8],
  146.             intpixels[i+9]);
  147.         getch();
  148.         }
  149.  
  150.     }
  151.  
  152. }
  153.  
  154. demoscreen(int rows, int cols, int colors)
  155. {
  156. unsigned int i, j, k, l, r, g, b;
  157.  
  158. /* generate a simple image - then, for 16/256 color modes, color-cycle it */
  159. /* exit when a key is pressed */
  160.  
  161. if (colors > 0) {            /* 16 and 256 color modes */
  162.     xga_setpalette(&palette[16]);        /* set the palette */
  163.     k = 0;
  164.     for (j = 0; j < cols; j++) {    /* generate a test pattern */
  165.         if (++k >= colors) k = 0;
  166.         l = k;
  167.         for (i = 0; i < rows; i++) {
  168.             if (++l >= colors) l = 0;
  169.             pixels[i] = l;
  170. /*
  171.             xga_putpixel(i, j, l);
  172. */
  173.             }
  174.         xga_putline(j, 0, rows-1, pixels);
  175.         if (kbhit()) {        /* user bail-out */
  176.             break;
  177.             }
  178.         }
  179.  
  180. /*    testing - cycle the palette until a key is hit */
  181.     i = 16;
  182.     while (kbhit() == 0) {
  183.         if (++i >= 256) i = 0;
  184.         xga_setpalette(&palette[i]);
  185.         }
  186.     }
  187. else    {                /* 65536 "true color" mode */
  188.     g = 0;
  189.     for (j = 0; j < cols; j++) {    /* generate a test pattern */
  190.         if (++g >= 64) g = 0;
  191.         r = 0;
  192.         b = 0;
  193.         if (j >= 256) b = 16;
  194.         for (i = 0; i < rows; i++) {
  195.             if (++r >= 32) {
  196.                 r = 0;
  197.                 if (++b >= 32) b = 0;
  198.                 }
  199.             l = (r << 11) + (g << 5) + b;
  200.             if ((i < 3 || i >= rows-3) ||
  201.                 (j < 3 || j >= cols-3)) 
  202.                 l = (31 << 11) + (63 << 5) + 31;
  203.             intpixels[i] = l;
  204. /*
  205.             xga_putpixel(i,j,l);
  206. */
  207.             }
  208.         xga_putline(j, 0, rows-1, intpixels);
  209.         if (kbhit()) {        /* user bail-out */
  210.             break;
  211.             }
  212.         }
  213.  
  214.     }
  215.  
  216. /* read back a row, just for test purposes */
  217. /*
  218. for (i = 0; i < rows; i++)
  219.     intpixels[i] = xga_getpixel(i, 2);
  220. */
  221. if (colors > 0) {
  222.     xga_getline(2, 0, rows-1, pixels);
  223.     for (i = 0; i < rows; i++)
  224.         intpixels[i] = pixels[i];
  225.     }
  226. else
  227.     xga_getline(2, 0, rows-1, intpixels);
  228.  
  229. }
  230.